﻿using DICOMViewer.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DICOMViewer.Utility
{
    public class ApplicationArgsUtility
    {
        private  const int SW_MAXIMIZE = 3;
        private  const int SW_RESTORE = 9;
        public static void Excute(string[] args, Form form)
        {
            ArgsModel arg = ArgsModel.Parse(args);

            if (LoginInfo.Current != null)
            { 
                if (LoginInfo.Current.Token != arg.Token) return;
                if (form.Visible)
                {
                    (form as MainForm).ShowOrderByOrderNumber(arg.OrderNumber);
                    //do somethings
                    WinMessageUtility.ShowWindowAsync(form.Handle, SW_MAXIMIZE);// SW_MAXIMIZE);//显示
                    WinMessageUtility.SetForegroundWindow(form.Handle);//当到最前端
                }
            }
            else//尚未登录
            {
                bool isAuc = LoginUtility.CheckLoginByToken(arg.UUID, arg.Token);
                if (isAuc)
                {
                    form.Hide();
                    MainForm mainform = new MainForm();
                    mainform.Show();
                    WinMessageUtility.ShowWindowAsync(mainform.Handle, SW_MAXIMIZE);// SW_MAXIMIZE);//显示
                    WinMessageUtility.SetForegroundWindow(mainform.Handle);//当到最前端

                }
                else
                {
                    MessageBox.Show("token login failed!");
                }
            }
        }

        public static void Excute(string[] args)
        {
            if (args.Length==0)
            {
                Application.Run(new Login());
                return;}
            ArgsModel arg = ArgsModel.Parse(args);
            bool isAuc = LoginUtility.CheckLoginByToken(arg.UUID, arg.Token);
            if (isAuc)
            {
                //运行主界面
                Application.Run(new MainForm( arg));

            }
        }
        /// <summary>
        /// 发动带命令行的win消息
        /// </summary>
        /// <param name="args">命令参数</param>
        /// <param name="hwndRecWindow">接收句柄</param>
        public  static void SendArgsWinMessage(string[] args,IntPtr hwndRecWindow)
        {
            string strText = string.Empty;
            foreach (string s in args)
            {
                strText = strText  + s +" ";
            }
            strText = strText.Trim();
            //自己的进程句柄
            IntPtr hwndSendWindow = Process.GetCurrentProcess().Handle;

            //填充COPYDATA结构
            DICOMViewer.Utility.CopyDataStruct copydata = new DICOMViewer.Utility.CopyDataStruct();
            copydata.cbData = Encoding.Default.GetBytes(strText).Length; //长度 注意不要用strText.Length;  
            copydata.lpData = strText; //内容  

            //发送消息
            DICOMViewer.Utility.WinMessageUtility.SendMessage(hwndRecWindow, DICOMViewer.Utility.WinMessageUtility.WM_COPYDATA,
                hwndSendWindow, ref copydata);
        }
    }

    public class ArgsModel
    {
        public string Command { get; set; }
        public string UUID { get; set; }
        public string Token { get; set; }
        public string OrderNumber { get; set; }
        public string[] Params { get; set; }
        public static ArgsModel Parse(string[] args)
        {
            ArgsModel model = new ArgsModel();
            if (args.Length % 2 != 0) return null;
            for (int i = 0; i < args.Length; i=i+2)
            {
                switch (args[i].ToLower())
                {
                    case "-c":
                        model.Command = args[i + 1];
                        break;
                    case "-n":
                        model.OrderNumber = args[i + 1];
                        break;
                    case "-u":
                        model.UUID = args[i + 1];
                        break;
                    case "-t":
                        model.Token = args[i + 1];
                        break;
                    case "-p":
                        model.Params = args[i + 1].Split(' ');
                        break;
                }
            }
            return model;
        }
    }
}
